package com.obrempon.credit.market.utils.helper

import com.obrempon.credit.market.utils.ObremponDateUtil
import com.obrempon.credit.market.utils.shared.ObremponSharedHelper
import com.obrempon.credit.market.utils.shared.ObremponSharedKey
import org.json.JSONObject
import java.util.Calendar

/**
 * ProcessHelper
 * @Author wwdeng
 * @CreateTime: 2024/11/19 10:44
 * @Annotation：用户流程状态 帮助类
 */
object ObremponProcessHelper {

    /**
     * 保存用户流程数据
     */
    fun saveObremponAuthData(data: JSONObject){
        ObremponSharedHelper.addObremponSharedData(ObremponSharedKey.OBREMPON_PROCESS, data.toString())
    }

    /**
     * 获取流程数据
     */
    fun takeObremponAuthData(): JSONObject? {
        val dataString = ObremponSharedHelper.queryObremponSharedString(ObremponSharedKey.OBREMPON_PROCESS)
        if (dataString.isEmpty()){
            return null
        }
        return JSONObject(dataString)
    }

    /**
     * 获取所有流程titleList
     */
    fun takeObremponAuthAllStepTitleList(): ArrayList<String> {
        val titleList = arrayListOf<String>()
        val data = takeObremponAuthData() ?: return titleList

        val allStepTitles = data.optJSONArray("allStepTitles")
        if (allStepTitles != null) {
            for (i in 0..<allStepTitles.length()){
                titleList.add(allStepTitles.optString(i))
            }
        }
        return titleList
    }

    /**
     * 获取总流程步骤key
     */
    fun takeObremponAuthAllStepList(): ArrayList<String>{
        val allStepList = arrayListOf<String>()
        val data = takeObremponAuthData() ?: return allStepList
        val allSteps = data.optJSONArray("allSteps")
        if (allSteps != null){
            for (i in 0..<allSteps.length()){
                allStepList.add(allSteps.optString(i))
            }
        }
        return allStepList
    }

    /**
     * 获取剩余未完成的流程列表
     */
    fun takeObremponAuthStepList(): ArrayList<String>{
        val stepList = arrayListOf<String>()
        val data = takeObremponAuthData() ?: return stepList
        val steps = data.optJSONArray("steps")
        if (steps != null){
            for (i in 0..<steps.length()){
                stepList.add(steps.optString(i))
            }
        }
        return stepList
    }

    /**
     * 获取获取当前进行的步骤下标
     */
    fun takeObremponAuthStepIndex(): Int{
        var stepIndex = takeObremponAuthAllStepList().size
        if (takeObremponAuthStepList().size == 0){
            //所有步骤已完成
            return stepIndex
        }
        val curtStep = takeObremponAuthStepList()[0]
        for (i in 0..<stepIndex){
            if (curtStep == takeObremponAuthAllStepList()[i]){
                stepIndex = i
                break
            }
        }
        return stepIndex
    }

    /**
     * 获取订单状态
     */
    fun takeObremponAuthOrderStatus(): String{
        val data = takeObremponAuthData() ?: return ""
        return data.optString("orderStatus","")
    }

    /**
     * 获取申请状态
     */
    fun takeObremponAuthApplyStatus(): String{
        val data = takeObremponAuthData() ?: return ""
        return data.optString("applyStatus","")
    }

    /**
     * 授信状态
     */
    fun takeObremponAuthBeforeRiskStatus(): String{
        val data = takeObremponAuthData() ?: return ""
        return data.optString("beforeRiskStatus","")
    }

    /**
     * 是否是可以修改手机号
     */
    fun isObremponAuthMobileEditFlag(): Boolean{
        val data = takeObremponAuthData() ?: return false
        return data.optString("mobileEditFlag") == "YES"
    }

    /**
     * 是否开放WhatsApp手机校验方式  YES、NO
     */
    fun isObremponAuthCheckWhatsApp(): Boolean{
        val data = takeObremponAuthData() ?: return false
        return data.optString("whatsappFlag") == "YES"
    }

    /**
     * 是否开放SMS 手机校验方式  YES、NO
     */
    fun isObremponAuthCheckSMS(): Boolean{
        val data = takeObremponAuthData() ?: return false
        return data.optString("smsFlag") == "YES"
    }

    /**
     * 获取可再次申请时间
     */
    fun takeObremponAuthApplyAgainDate(): String{
        val data = takeObremponAuthData() ?: return ""
        return data.optString("againApplyDate")
    }

    /**
     * 获取授信到期时间
     */
    fun takeObremponAuthCreditExpireDate(): String{
        val data = takeObremponAuthData() ?: return ""
        if (!data.has("creditExpireDate")){
            val calendar = Calendar.getInstance()
            calendar.add(Calendar.YEAR,1)
            return ObremponDateUtil.getFormatTime(calendar.timeInMillis,"MM/dd/yyyy")
        }
        return data.optString("creditExpireDate")
    }
}